package com.bosssoft.hr.train.springmvc.example.controller;


import com.bosssoft.hr.train.springmvc.example.model.User;
import com.bosssoft.hr.train.springmvc.example.util.result.Result;
import com.bosssoft.hr.train.springmvc.example.service.UserService;
import com.bosssoft.hr.train.springmvc.example.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {
    /**
    @Autowired
    UserService userService;

    @GetMapping("/list")
    @ResponseBody
    public Result<User> listAllUser(){
        return ResultUtil.success(userService.listAll());
    }

    @PostMapping("/login_rest")
    @ResponseBody
    public Result<Boolean> login(@RequestBody User user){
        return ResultUtil.success(userService.login(user.getName(),user.getPassword()));
    }

    @PostMapping("/login")
    public ModelAndView loginJSP(User user, HttpServletRequest req){
        log.info(user.toString());
        ModelAndView mv = new ModelAndView();
        HttpSession session = req.getSession();
        if(userService.login(user.getName(),user.getPassword())){
            session.setAttribute("name",user.getName());
            mv.setViewName("main");
        }else {
            mv.addObject("msg","登录失败请重新登录");
            mv.setViewName("login");
        }
        return mv;
    }

    @PostMapping("/add")
    @ResponseBody
    public Result<Boolean> add(@RequestBody User user){
        return ResultUtil.success(userService.insert(user));
    }

    @GetMapping("/remove")
    @ResponseBody
    public Result<Boolean> remove(@RequestParam("id") Long id){
        return ResultUtil.success(userService.remove(id));
    }

    @PostMapping("/update")
    @ResponseBody
    public Result<Boolean> update(@RequestBody User user){
        return ResultUtil.success(userService.update(user));
    }
**/

}
